

// 1.断言：强制转换一个变量的类型,(转换的后果需要开发者清楚和承担)
let num = 10;

// <>写法 不提倡
let a = (<any>num)

// as关键字  as：把... 当作...
let b = (num as any)

let num1: any = 1
let c = num1 as string

// 2.断言的限制,只有充分重叠的类型才能够互相断言（转换）
// num as string
interface IPerson {
    name: string,
    // age: number
}

interface IStudent {
    name: string,
    score: number,
}

let p1: IPerson = { name: "oo" }
let s1: IStudent = { name: "oo", score: 100 }

let s2 = p1 as IStudent
let p2 = s1 as IPerson

console.log(s2.score)//undefined
console.log(s1.score)//100


// 非空断言
type UnionType = string | undefined | null
let s: UnionType = "hello"

function fn(arg: UnionType) {
    // ?表示可选 arg中有split就执行， 没有就不执行
    console.log(arg?.split(''))

    // !代表 非空断言， 把变量中 为undefined和null 的情况给排除掉
    console.log(arg!.split(''))
}

fn(s)
fn(undefined)
export default null